<?php

/**
 * This is the model class for table "tbl_template".
 *
 * The followings are the available columns in table 'tbl_template':
 * @property integer $templateId
 * @property integer $templateCategoryId
 * @property integer $userId
 * @property integer $articleId
 * @property integer $templateType
 * @property string $templateTitle
 * @property string $templateSummary
 * @property string $templateBody
 * @property integer $buyTimes
 * @property string $prices
 * @property integer $totalWords
 * @property string $created
 */
class Template extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Template the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_template';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('templateCategoryId, userId, articleId, templateType, templateTitle, templateSummary, templateBody, buyTimes, prices, totalWords, created', 'required'),
			array('templateCategoryId, userId, articleId, templateType, buyTimes, totalWords', 'numerical', 'integerOnly'=>true),
			array('templateTitle', 'length', 'max'=>100),
			array('templateSummary', 'length', 'max'=>500),
			array('prices', 'length', 'max'=>2),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('templateId, templateCategoryId, userId, articleId, templateType, templateTitle, templateSummary, templateBody, buyTimes, prices, totalWords, created', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'templateId' => 'Template',
			'templateCategoryId' => 'Template Category',
			'userId' => 'User',
			'articleId' => 'Article',
			'templateType' => 'Template Type',
			'templateTitle' => 'Template Title',
			'templateSummary' => 'Template Summary',
			'templateBody' => 'Template Body',
			'buyTimes' => 'Buy Times',
			'prices' => 'Prices',
			'totalWords' => 'Total Words',
			'created' => 'Created',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		if(isset($this->_keywords))
		{
			$searchCondition = explode(",",$this->_keywords);
// 			$criteria1=new CDbCriteria;
// 			$criteria1->select='templateId';  
// 			$criteria1->condition='keywordName in ?';
// 			$criteria1->params=$searchCondition;
// 			$keys = Keyword::model()->findAll($criteria1);
// 			
			$keys = Keyword::model()->findAllByAttributes(array('keywordName' => $searchCondition));
			$templateIds = array();
			foreach ($keys as $value)
			{
				array_push($templateIds,$value->templateId);
			}
		}
		//$criteria->compare('templateId',$this->templateId);
		$criteria->compare('templateCategoryId',$this->templateCategoryId);
		if(isset($templateIds))
			$criteria->addInCondition('templateId',$templateIds);
		
		$criteria->order='created DESC';
		$criteria->limit = 10000;
//		$criteria->compare('userId',$this->userId);
//		$criteria->compare('templateType',$this->templateType);
//		$criteria->compare('templateBody',$this->templateBody,true);
//		$criteria->compare('buyTimes',$this->buyTimes);
//		$criteria->compare('prices',$this->prices,true);
//		$criteria->compare('created',$this->created,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
					'pageSize'=>5,
					),
		));
	}
}